package com.gobang.common.exception;

/**
 * 统一错误码枚举
 * 根据业务模块分类，便于错误定位和处理
 * 
 * 错误码规则：
 * - 系统级错误：1000-1999
 * - 用户管理模块：2000-2999  
 * - 认证授权模块：3000-3999
 * - 积分管理模块：4000-4999
 * - 匹配系统模块：5000-5999
 * - 游戏核心模块：6000-6999
 * - 实时通信模块：7000-7999
 * 
 * @author System
 * @since 1.0.0
 */
public enum ErrorCode {

    // ============ 系统级错误 1000-1999 ============
    SUCCESS("1000", "操作成功"),
    SYSTEM_ERROR("1001", "系统内部错误"),
    PARAM_ERROR("1002", "参数错误"),
    PARAM_VALIDATE_ERROR("1003", "参数校验失败"),
    DATA_NOT_FOUND("1004", "数据不存在"),
    DATA_ALREADY_EXISTS("1005", "数据已存在"),
    OPERATION_FAILED("1006", "操作失败"),
    DATABASE_ERROR("1007", "数据库操作异常"),
    NETWORK_ERROR("1008", "网络异常"),
    TIMEOUT_ERROR("1009", "操作超时"),

    // ============ 用户管理模块 2000-2999 ============
    USER_NOT_FOUND("2001", "用户不存在"),
    USERNAME_ALREADY_EXISTS("2002", "用户名已存在"),
    USER_CREATE_FAILED("2003", "用户创建失败"),
    USER_UPDATE_FAILED("2004", "用户信息更新失败"),
    USER_DELETE_FAILED("2005", "用户删除失败"),
    USERNAME_INVALID("2006", "用户名格式不正确"),
    PASSWORD_INVALID("2007", "密码格式不正确"),
    USER_STATUS_INVALID("2008", "用户状态异常"),
    USER_SCORE_UPDATE_FAILED("2009", "用户积分更新失败"),
    USER_GAME_STATS_ERROR("2010", "用户游戏统计数据异常"),

    // ============ 认证授权模块 3000-3999 ============
    LOGIN_FAILED("3001", "登录失败"),
    USERNAME_OR_PASSWORD_ERROR("3002", "用户名或密码错误"),
    USER_NOT_LOGIN("3003", "用户未登录"),
    LOGIN_EXPIRED("3004", "登录已过期"),
    TOKEN_INVALID("3005", "无效的访问令牌"),
    TOKEN_EXPIRED("3006", "访问令牌已过期"),
    PERMISSION_DENIED("3007", "权限不足"),
    ACCOUNT_LOCKED("3008", "账户已被锁定"),
    PASSWORD_ENCRYPT_ERROR("3009", "密码加密失败"),
    SESSION_CREATE_FAILED("3010", "会话创建失败"),

    // ============ 积分管理模块 4000-4999 ============
    SCORE_CALCULATE_ERROR("4001", "积分计算错误"),
    SCORE_UPDATE_FAILED("4002", "积分更新失败"),
    SCORE_RECORD_CREATE_FAILED("4003", "积分记录创建失败"),
    SCORE_RECORD_NOT_FOUND("4004", "积分记录不存在"),
    SCORE_INSUFFICIENT("4005", "积分不足"),
    SCORE_CHANGE_INVALID("4006", "无效的积分变化"),
    SCORE_HISTORY_QUERY_FAILED("4007", "积分历史查询失败"),
    SCORE_ROLLBACK_FAILED("4008", "积分回滚失败"),
    SCORE_CONCURRENCY_ERROR("4009", "积分并发更新冲突"),
    SCORE_VALIDATE_ERROR("4010", "积分数据校验失败"),

    // ============ 匹配系统模块 5000-5999 ============
    MATCH_QUEUE_JOIN_FAILED("5001", "加入匹配队列失败"),
    MATCH_QUEUE_LEAVE_FAILED("5002", "退出匹配队列失败"),
    MATCH_ALREADY_IN_QUEUE("5003", "已在匹配队列中"),
    MATCH_NOT_IN_QUEUE("5004", "未在匹配队列中"),
    MATCH_OPPONENT_NOT_FOUND("5005", "未找到合适的对手"),
    MATCH_TIMEOUT("5006", "匹配超时"),
    MATCH_CANCELLED("5007", "匹配已取消"),
    MATCH_ALGORITHM_ERROR("5008", "匹配算法错误"),
    MATCH_ROOM_CREATE_FAILED("5009", "创建游戏房间失败"),
    MATCH_USER_STATUS_ERROR("5010", "用户状态不允许匹配"),
    MATCH_REQUEST_FAILED("5011", "匹配请求失败"),
    MATCH_CANCEL_FAILED("5012", "取消匹配失败"),

    // ============ 游戏核心模块 6000-6999 ============
    GAME_ROOM_NOT_FOUND("6001", "游戏房间不存在"),
    GAME_ROOM_FULL("6002", "游戏房间已满"),
    GAME_NOT_STARTED("6003", "游戏未开始"),
    GAME_ALREADY_STARTED("6004", "游戏已开始"),
    GAME_ALREADY_ENDED("6005", "游戏已结束"),
    GAME_MOVE_INVALID("6006", "无效的落子操作"),
    GAME_NOT_YOUR_TURN("6007", "当前不是您的回合"),
    GAME_POSITION_OCCUPIED("6008", "该位置已有棋子"),
    GAME_POSITION_OUT_OF_BOUNDS("6009", "落子位置超出棋盘范围"),
    GAME_BOARD_STATE_ERROR("6010", "棋盘状态异常"),
    GAME_PLAYER_NOT_IN_ROOM("6011", "玩家不在该游戏房间中"),
    GAME_SURRENDER_FAILED("6012", "认输操作失败"),
    GAME_WIN_CHECK_ERROR("6013", "胜负判定错误"),
    GAME_STATE_SYNC_ERROR("6014", "游戏状态同步失败"),
    GAME_TIMER_ERROR("6015", "游戏计时器异常"),

    // ============ 实时通信模块 7000-7999 ============
    WEBSOCKET_CONNECTION_FAILED("7001", "WebSocket连接失败"),
    WEBSOCKET_MESSAGE_SEND_FAILED("7002", "消息发送失败"),
    WEBSOCKET_SESSION_NOT_FOUND("7003", "WebSocket会话不存在"),
    WEBSOCKET_SESSION_EXPIRED("7004", "WebSocket会话已过期"),
    MESSAGE_FORMAT_ERROR("7005", "消息格式错误"),
    MESSAGE_TYPE_UNSUPPORTED("7006", "不支持的消息类型"),
    REAL_TIME_SYNC_FAILED("7007", "实时同步失败"),
    CONNECTION_HEARTBEAT_TIMEOUT("7008", "连接心跳超时"),
    BROADCAST_MESSAGE_FAILED("7009", "广播消息失败"),
    USER_OFFLINE("7010", "用户已离线");

    /**
     * 错误码
     */
    private final String code;

    /**
     * 错误消息
     */
    private final String message;

    ErrorCode(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    /**
     * 根据错误码获取ErrorCode枚举
     *
     * @param code 错误码
     * @return ErrorCode枚举，如果未找到返回SYSTEM_ERROR
     */
    public static ErrorCode getByCode(String code) {
        for (ErrorCode errorCode : ErrorCode.values()) {
            if (errorCode.getCode().equals(code)) {
                return errorCode;
            }
        }
        return SYSTEM_ERROR;
    }
} 